package com.hz.userService.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.hz.userService.domain.Grade;
import com.hz.userService.service.GradeService;
import com.hz.userService.mapper.GradeMapper;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.Map;

/**
* @author li
* @description 针对表【grades(年级表)】的数据库操作Service实现
* @createDate 2023-09-23 17:13:26
*/
@Service
public class GradeServiceImpl extends ServiceImpl<GradeMapper, Grade>
    implements GradeService{
    @Resource
    GradeMapper gradeMapper;
    @Override
    public Boolean addGrade(String gradeName,Integer classNumber, String gradeYear) {
        Grade grade = new Grade();

        /* 判断年级名称是否重复 */
        Map<String,Object> map = new HashMap<>();
        map.put("grade_name",gradeName);
        if (gradeMapper.selectByMap(map).size() != 0)
            return false;

        /* 添加年级的数据 */
        grade.setGradeName(gradeName);
        grade.setClassNumber(classNumber);
        grade.setGradeYear(gradeYear);

        return gradeMapper.insert(grade)!=0;
    }

    @Override
    public Boolean editGrade(Integer id, String gradeName, Integer classNumber,String gradeYear) {
        /* 通过id找到对应的grade */
        Grade grade = gradeMapper.selectById(id);

        /* 若未找到用户，返回false */
        if (grade == null)
            return false;

        /* 修改grade的数据 */
        grade.setGradeName(gradeName);
        grade.setClassNumber(classNumber);
        grade.setGradeYear(gradeYear);

        return gradeMapper.updateById(grade) != 0;
    }

    @Override
    @CacheEvict(value = "grade_", key = "#id")
    public Boolean deleteGrade(Integer id) {

        /* 删除成功返回true 失败返回false */
        return gradeMapper.deleteById(id) != 0;

    }

    @Override
    @Cacheable(value = "grade_",key = "#id")
    public Grade viewGrade(Integer id) {
        return gradeMapper.selectById(id);
    }
}




